package cn.azzhu.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author azzhu
 * @create 2020-08-18 10:26:55
 */
//@Component
public class MyHandlerInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取session
//        final HttpSession session = request.getSession();
//        //从session中获取值
//        final Object loginUser = session.getAttribute("loginUser");
//        if(loginUser != null) {
//            //处理登录,放行
//            return true;
//        }
//        response.sendRedirect(".....");
        System.out.println("[First]----preHandle");
        System.out.println(handler);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("[First]----postHandle");
        System.out.println(handler);
        System.out.println(modelAndView.getViewName()+","+modelAndView.getModel());
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("[First]----afterCompletion");
        System.out.println(handler);
    }
}
